package com.example.database_manage.start_load;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import com.example.database_manage.R;
import com.example.database_manage.administractor.Container;
import com.example.database_manage.database.CommonDatabase;


import static com.example.database_manage.database.MD5Demo.md5;

/***
 * 登录界面的配置
 */
public class load extends AppCompatActivity {


    private SQLiteDatabase db;
    private SharedPreferences pref;//声明信息保护类
    private SharedPreferences.Editor editor;
    private CheckBox rememberPass;
    private boolean isRemember;
    private EditText zhanghao;      //声明输入账号控件
    private EditText password;      //声明输入密码
    //SharedPreferences mySharePreferences;


    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load);

        //用于记住密码的实现
        rememberPass = findViewById(R.id.remeber_passsword);
        pref = PreferenceManager.getDefaultSharedPreferences(this);
        isRemember = pref.getBoolean("remember_password", false);


        //账号密码编辑框
        zhanghao = findViewById(R.id.edit_load_zhanghao);

        password = findViewById(R.id.edit_load_password);
        //获取数据库对象
        CommonDatabase commonDatabase = new CommonDatabase();

        db = commonDatabase.getSqliteObject(load.this, "test_db");

        //绑定组件
        final Button button_change_password = findViewById(R.id.button_change_password);

        final ImageButton imageButton = findViewById(R.id.yanjing);

        //眼睛睁开闭上
        password.setTransformationMethod(PasswordTransformationMethod.getInstance());//初始为隐藏密文状态

        imageButton.setOnTouchListener(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        int action = event.getAction();
                        if (v.getId() == R.id.yanjing) {
                            switch (action) {
                                case MotionEvent.ACTION_DOWN://按下（按下动作）
                                    imageButton.setSelected(true);
                                    //密码可见
                                    ((ImageButton) v).setImageDrawable(getResources().getDrawable(R.drawable.eye));
                                    password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                                    break;
                                case MotionEvent.ACTION_UP://抬起（抬起动作）
                                    imageButton.setSelected(false);
                                    ((ImageButton) v).setImageDrawable(getResources().getDrawable(R.drawable.noeye));
                                    //密码不可见
                                    password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                                    break;
                            }
                        }
                        return true;
                    }


        });


        //登录按钮
        Button button_load = findViewById(R.id.button_load);
        button_load.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //获取输入的账号密码
                String account_load = zhanghao.getText().toString();
                String password_load = md5(password.getText().toString());
                if(TextUtils.isEmpty(account_load) || TextUtils.isEmpty(password_load)){
                    Toast.makeText(load.this, "账号或密码不可为空", Toast.LENGTH_SHORT).show();
                }
                else {
                    String true_password = "";
                    Cursor cursor = db.query("administractor", null, "account = ?", new String[]{account_load}, null, null, null);

                    while (cursor.moveToNext()) {
                        true_password = cursor.getString(cursor.getColumnIndex("password"));
                    }

                    if (password_load.equals(true_password)) {
                        isr();
                        startActivity(new Intent(load.this, Container.class));
                        Toast.makeText(load.this, "登录成功！", Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        Toast.makeText(load.this, "密码错误！", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        //点击修改按钮进入更改密码的界面

        button_change_password.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ClickableViewAccessibility")
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(load.this, password_change.class);
                startActivity(intent);
            }
        });

        //如果手机有记录
        if (isRemember) {
            zhanghao.setText(pref.getString("account", ""));
            password.setText(pref.getString("password", ""));
            //
            rememberPass.setChecked(true);
        }
    }

    //用于实现记住密码的方法，这里使用SharedPreference存放临时数据
    private void isr() {
        editor = pref.edit();
        //检查复选框是否被选中
        if (rememberPass.isChecked()) {
            editor.putBoolean("remember_password", true);
            editor.putString("account", zhanghao.getText().toString());
            editor.putString("password", password.getText().toString());
        } else {
            editor.clear();
        }
        editor.apply();
    }

}
